package com.stars.empoly.vmodel

import android.app.Application
import android.os.Bundle
import androidx.lifecycle.MutableLiveData
import com.stars.empoly.activity.RechargeActivity
import com.stars.empoly.activity.WithdrawalActivity
import com.stars.empoly.config.MIntentAction
import com.stars.empoly.fragment.AccountBillFragment
import com.stars.empoly.model.AmountCount
import com.stars.empoly.model.User
import com.stars.empoly.utils.net.NetEngine
import com.trailer.base.base.BRRepository
import com.trailer.base.base.BRViewModel
import com.trailer.base.net.IResp
import com.trailer.base.utils.UiSwitch

/**
 *
 *on 2020/9/18
 */
class AccountBillVModel(application: Application) : BRViewModel<BRRepository>(application) {


    val user: MutableLiveData<User?> = MutableLiveData(User.get())
    var info: MutableLiveData<AmountCount> = MutableLiveData<AmountCount>()

    var fragment: AccountBillFragment? = null
    var status = "0"


    fun getAmountCount() {

        repository().rxPreq(NetEngine.service.amountCount())
                .doOnNext {
                    if (it.ok(false)) {
                        var bean = it.data() as AmountCount
                        if (null != bean) {
                            info.value = bean
                        }

                    }
                }
                .subscribe()
    }


    fun goWithdrawal() {//提现

        UiSwitch.bundleRes(fragment, WithdrawalActivity::class.java, Bundle().apply {
            putString("money", info.value!!.can_use)
        }, MIntentAction.REQUEST_CODE_ONE)

    }

    fun toBillDetails() {//账单明细

       // UiSwitch.single(repository().context, BillDetailsActivity::class.java)

    }

    fun toRecharge() {//去充值

        UiSwitch.singleRes(fragment, RechargeActivity::class.java, MIntentAction.REQUEST_CODE_ONE)
    }

    override fun apiFunc(): suspend () -> IResp<*> {
        return { NetEngine.service.amountList( status, "","","",repository().page) }
    }
}
